import { _decorator, Component, game, Node } from 'cc';
import { GlobalVar } from '../GlobalVar';
import { Config } from '../Config';
import { GameEvent, PropUpType } from '../EventName';
const { ccclass, property } = _decorator;

@ccclass('HttpCom')
export class HttpCom {
    // 发送 GET 请求的方法

    public static get(url: string, callback: (response: any) => void) {
        game.emit(GameEvent.SHOW_LOADING_PAGE, true);
        let xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            console.log(xhr.responseText, url)
            if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
                try {
                    game.emit(GameEvent.SHOW_LOADING_PAGE, false);
                    let response = JSON.parse(xhr.responseText);
                    callback(response);
                    console.info("code:", response.code, "msg:", response.msg);
                } catch (e) {
                    console.error("Failed to parse response JSON:", e);

                }
            }

            if (xhr.status == 500) {

            }
        };
        xhr.open("GET", url, true);
        xhr.setRequestHeader("Authorization", "Bearer " + GlobalVar.token); // 设置 Header
        xhr.send();
    }

    // 发送 POST 请求的方法
    public static post(url: string, data: any, callback: (response: any) => void) {
        game.emit(GameEvent.SHOW_LOADING_PAGE, true);
        let xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            console.log(xhr.responseText, url)
            if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
                try {
                    game.emit(GameEvent.SHOW_LOADING_PAGE, false);
                    let response = JSON.parse(xhr.responseText);
                    callback(response);
                    console.info("code:", response.code, "msg:", response.msg);
                } catch (e) {
                    console.error("Failed to parse response JSON:", e);
                }
            }
        };
        xhr.open("POST", url, true);
        xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
        xhr.setRequestHeader("Authorization", "Bearer " + GlobalVar.token); // 设置 Header
        let str = JSON.stringify(data);
        console.log(str, "postData")
        xhr.send(str);
    }


    public static getUserInfo(cb?) {
        HttpCom.get(Config.getInfo, (resp) => {
            //console.log(resp,"用户信息")

            GlobalVar.userInfo = resp.data.user;

            game.emit(GameEvent.USER_INFO_UPDATE)
            console.log(GlobalVar.userInfo, "用户信息");
            cb && cb();
        })
    }



    public static cdkVerify(cdk: string) {
        HttpCom.post(Config.cdkVerify, { cdk: cdk }, (resp) => {

            //GlobalVar.userInfo = resp.data.user;
            //game.emit(GameEvent.USER_INFO_UPDATE)
            console.log(resp, "CDK信息");
            if (resp.code == 500) {
                console.log('CDK无效')
                game.emit(GameEvent.SHOW_TOAST, resp.msg)
                return
            }

            if (resp.code == 200) {
                game.emit(GameEvent.SHOW_REWARD_PROPUP, resp, PropUpType.CDKReward);

                HttpCom.getUserInfo();
            }
        })
    }

    public static getBossAward() {
        HttpCom.post(Config.getBossAward, null, (resp) => {

            //GlobalVar.userInfo = resp.data.user;
            //game.emit(GameEvent.USER_INFO_UPDATE)
            console.log(resp, "boss奖励");
            if (resp.code == 500) {
                console.log('boss奖励无效')
                return
            }

            if (resp.code == 200) {

                HttpCom.getUserInfo();
            }
        })
    }

    public static signinList() {
        HttpCom.get(Config.signinList, (resp) => {
            console.log(resp, "签到列表");
            if (resp.code == 500) {
                console.log('')
                return
            }

            if (resp.code == 200) {
                game.emit(GameEvent.ON_SIGNDATA_UPDATE, resp.rows)
                //HttpCom.getUserInfo();
                HttpCom.userSigninList();
            }
        })
    }
    public static signin(cb: Function) {
        HttpCom.post(Config.signin, null, (resp) => {
            console.log(resp, "用户签到");
            if (resp.code == 500) {
                console.log('用户签到失败')
                return
            }

            if (resp.code == 200) {
                console.log('用户签到成功')
                if (cb) {
                    cb();
                }
                HttpCom.getUserInfo();
            }
        })
    }


    public static userSigninList() {
        HttpCom.get(Config.userSigninList, (resp) => {
            console.log(resp, "用户签到列表");
            if (resp.code == 500) {
                console.log('')
                return
            }

            if (resp.code == 200) {
                game.emit(GameEvent.ON_USER_SIGNDATA_UPDATE, resp);
                HttpCom.isSignin()
                //HttpCom.getUserInfo();
            }
        })
    }
    //用户今天是否已签到
    public static isSignin() {
        HttpCom.get(Config.isSignin, (resp) => {
            console.log(resp, "用户今天是否已签到");
            if (resp.code == 500) {
                console.log('')
                return
            }

            if (resp.code == 200) {
                game.emit(GameEvent.ON_CHECK_IS_SIGNED, resp);
                //HttpCom.getUserInfo();
            }
        })
    }
    //获取商店商品列表
    public static list(cb: Function) {
        console.log("获取商品列表")
        HttpCom.get(Config.list, (resp) => {
            console.log(resp, "商品列表");
            if (resp.code == 500) {
                console.log('')
                return
            }

            if (resp.code == 200) {
                cb(resp)
                ///game.emit(GameEvent.ON_CHECK_IS_SIGNED,resp);
                //HttpCom.getUserInfo();
            }
        })
    }
    //购买商品
    public static buy(cb: Function) {

        HttpCom.post(Config.buy, {}, (resp) => {
            console.log("购买商品");
            if (resp.code == 500) {
                console.log('')
                return
            }

            if (resp.code == 200) {
                console.log(resp, "购买商品返回");
                ///game.emit(GameEvent.ON_CHECK_IS_SIGNED,resp);
                //HttpCom.getUserInfo();
                cb && cb(resp);
            }
        })
    }
    public static taskList(cb: Function) {

        HttpCom.get(Config.taskList, (resp) => {
            console.log(resp, "任务列表");
            if (resp.code == 500) {
                console.log('')
                return
            }

            if (resp.code == 200) {
                cb(resp);
                ///game.emit(GameEvent.ON_CHECK_IS_SIGNED,resp);
                //HttpCom.getUserInfo();
            }
        })
    }
    public static userTaskList(cb: Function) {

        HttpCom.get(Config.userTaskList, (resp) => {
            console.log(resp, "用户任务列表");
            if (resp.code == 500) {
                console.log('')
                return
            }

            if (resp.code == 200) {
                cb && cb(resp);
                ///game.emit(GameEvent.ON_CHECK_IS_SIGNED,resp);
                //HttpCom.getUserInfo();
            }
        })
    }
    public static addX(cb?: Function) {

        HttpCom.post(Config.addX, null, (resp) => {
            if (resp.code == 500) {
                console.log('')
                return
            }

            if (resp.code == 200) {
                console.log(resp, "用户加入X成功");
                cb && cb(resp);
                ///game.emit(GameEvent.ON_CHECK_IS_SIGNED,resp);
                //HttpCom.getUserInfo();
                HttpCom.getUserInfo();
            }
        })
    }
    public static likeX(taskId: number, cb?: Function) {

        HttpCom.post(Config.likeX, { taskId: taskId }, (resp) => {

            if (resp.code == 500) {
                console.log('')
                return
            }

            if (resp.code == 200) {
                console.log(resp, "用户like-X成功");
                cb && cb(resp);
                ///game.emit(GameEvent.ON_CHECK_IS_SIGNED,resp);
                //HttpCom.getUserInfo();
                HttpCom.getUserInfo();
            }
        })
    }
    public static addTgChannel(cb?: Function) {

        HttpCom.post(Config.addTgChannel, null, (resp) => {

            if (resp.code == 500) {
                console.log('')
                return
            }

            if (resp.code == 200) {
                console.log(resp, "用户addTgChannel成功");
                cb && cb(resp);
                ///game.emit(GameEvent.ON_CHECK_IS_SIGNED,resp);
                //HttpCom.getUserInfo();
                HttpCom.getUserInfo();
            }
        })
    }
    //获取直推用户
    public static getChildList(cb?: Function) {
        console.log("获取直推用户");
        let params = "?pageSize=100&pageNum=1"
        HttpCom.get(Config.getChildList, (resp) => {

            if (resp.code == 500) {
                console.log('')
                return
            }

            if (resp.code == 200) {
                console.log(resp, "直推用户");
                cb && cb(resp);
                ///game.emit(GameEvent.ON_CHECK_IS_SIGNED,resp);
                //HttpCom.getUserInfo();
            }
        })
    }

    public static isLikeX(taskId: number, cb?: Function) {
        console.log("isLikeX");
        let params = "?pageSize=100&pageNum=1"
        let url = Config.isLikeX + "?taskId=" + taskId
        HttpCom.get(url, (resp) => {

            if (resp.code == 500) {
                console.log('')
                return
            }

            if (resp.code == 200) {
                console.log(resp, "LikeX任务是否完成");
                cb && cb(resp);
                ///game.emit(GameEvent.ON_CHECK_IS_SIGNED,resp);
                //HttpCom.getUserInfo();
            }
        })
    }
    public static doTask(taskId: number, cb?: Function) {
        console.log("doTask");
        // let params = "?pageSize=100&pageNum=1"
        // let url = Config.doTask + "?taskId=" + taskId
        let data = {
            taskId: taskId
        }
        HttpCom.post(Config.doTask, data, (resp) => {

            if (resp.code == 500) {
                console.log('')
                return
            }

            if (resp.code == 200) {
                console.log(resp, "doTask成功");
                cb && cb(resp);
                ///game.emit(GameEvent.ON_CHECK_IS_SIGNED,resp);
                //HttpCom.getUserInfo();
            }
        })
    }
    public static getReceiveAddress(cb?: Function) {
        console.log("获取收款地址");

        HttpCom.get(Config.getReceiveAddress, (resp) => {

            if (resp.code == 500) {
                console.log('')
                return
            }

            if (resp.code == 200) {
                console.log(resp, "收款地址返回");
                cb && cb(resp);
                ///game.emit(GameEvent.ON_CHECK_IS_SIGNED,resp);
                //HttpCom.getUserInfo();
            }
        })
    }

    public static tgLogin(initData: string, inviteCode: string, cb: Function) {
        console.log("tg登录")
        if (inviteCode) {

            HttpCom.post(Config.tgLogin, { initData: initData, inviteCode: inviteCode }, (resp) => {
                if (resp.code == 500) {
                    console.log('tg登录失败')
                    return
                }

                if (resp.code == 200) {
                    console.log(resp, "tg登录成功");
                    cb && cb(resp);
                    ///game.emit(GameEvent.ON_CHECK_IS_SIGNED,resp);
                    //HttpCom.getUserInfo();
                }
            })
        } else {

            HttpCom.post(Config.tgLogin, { initData: initData }, (resp) => {
                if (resp.code == 500) {
                    console.log('tg登录失败')
                    return
                }

                if (resp.code == 200) {
                    console.log(resp, "tg登录成功");
                    cb && cb(resp);
                    ///game.emit(GameEvent.ON_CHECK_IS_SIGNED,resp);
                    //HttpCom.getUserInfo();
                }
            })
        }
    }

    public static getRankList(cb?: Function) {
        HttpCom.get(Config.getRankList, (resp) => {
            cb && cb(resp);
        })
    }
    public static getRankHistoryList(cb?: Function) {
        HttpCom.get(Config.getRankHistoryList, (resp) => {

            if (resp.code == 500) {
                console.log('')
                return
            }

            if (resp.code == 200) {
                console.log(resp, "获取排行榜historyList");
                cb && cb(resp);
            }
        })
    }
    public static getAdventureAward(cb?: Function) {
        HttpCom.post(Config.getAdventureAward, {}, (resp) => {

            if (resp.code == 500) {
                console.log(resp, "获取危险之地奖励失败");
                return
            }

            if (resp.code == 200) {
                console.log(resp, "获取危险之地奖励成功");

                cb && cb(resp);
            }
        })
    }

    public static upgradeInfo(cb?: Function) {
        HttpCom.get(Config.upgradeInfo, (resp) => {

            if (resp.code == 500) {
                console.log(resp, "武器升级信息：失败");
                return
            }

            if (resp.code == 200) {
                console.log(resp, "武器升级信息：成功");

                cb && cb(resp);
            }
        })
    }

    public static unlock(cb?: Function) {
        HttpCom.post(Config.unlock, {}, (resp) => {

            if (resp.code == 500) {
                console.log(resp, "武器解锁：失败");
                return
            }

            if (resp.code == 200) {
                console.log(resp, "武器解锁：成功");

                cb && cb(resp);
            }
        })
    }
    public static upgradeWeapon(cb?: Function) {
        HttpCom.post(Config.upgradeWeapon, {}, (resp) => {

            if (resp.code == 500) {
                console.log(resp, "武器升级返回：失败");
                return
            }

            if (resp.code == 200) {
                console.log(resp, "武器升级返回：成功");

                cb && cb(resp);
            }
        })
    }
    public static getUnlockPrice(cb?: Function) {
        HttpCom.get(Config.getUnlockPrice, (resp) => {

            if (resp.code == 500) {
                console.log(resp, "解锁武器价格返回：失败");
                return
            }

            if (resp.code == 200) {
                console.log(resp, "解锁武器价格返回：成功");

                cb && cb(resp);
            }
        })
    }
    public static getWeapon(cb?: Function) {
        HttpCom.get(Config.getWeapon, (resp) => {

            if (resp.code == 500) {
                console.log(resp, "获取用户武器返回：失败");
                return
            }

            if (resp.code == 200) {
                console.log(resp, "获取用户武器返回：成功");

                cb && cb(resp);
            }
        })
    }



}


